package com.network.bio;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            try( Socket socket = new Socket()){
                socket.connect(new InetSocketAddress("localhost",8080));
                OutputStream outputStream = socket.getOutputStream();
                for(int i=0;i<10;i++){
                    outputStream.write((Thread.currentThread().getName()+"Hello World"+i).getBytes());
                    outputStream.flush();
                }
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        },"Tom");

        Thread thread2 = new Thread(() -> {
            try( Socket socket = new Socket()){
                socket.connect(new InetSocketAddress("localhost",8080));
                OutputStream outputStream = socket.getOutputStream();
                for(int i=0;i<10;i++){
                    outputStream.write((Thread.currentThread().getName()+"Hello World"+i).getBytes());
                    outputStream.flush();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        },"jerry");

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}
